package com.cs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @ClassName: NIOFileChannel02
 * @Description: TODO
 * @Author: kevinHan
 * @Date: 2020/8/6 21:17
 * @Version: 1.0
 */
public class NIOFileChannel02 {

    public static void main(String[] args) throws Exception {
        // 创建文件的输入流
        File file = new File("d:\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);

        //通过fileinputstream  获取对应的fileChannel -> 实际类型 FileChannelImpl
        FileChannel fileChannel = fileInputStream.getChannel();

        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());

        // 将通道的数据读入到buffer
        fileChannel.read(byteBuffer);

        // 将bytebuffer 字节数据 转换为 String
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }
}
